﻿var io = require('socket.io-client');
var robotInfo = require('./robot').getInstand;
var log = require("./../../util/loginfo").getInstand;
fanyingbiUser = function (_userInfo) {
    //相关属性
    this._userId = 0;
    this.RandCount = 0;
    this.TotalCount = Math.floor(Math.random() * 1200) + 60;
    //this.TotalCount = Math.floor(Math.random()*30) + 50;
    this.timer;

    this.isDown = false;

    //离线后再次登录时间
    this._online = true;
    this._out = false;
    this.setDownCoinMax = 0;

    this.goUp = false;

    this.chipsConfig = [100, 500, 1000, 5000, 10000, 50000];

    this.win = 0;
    this.winfalg = false;
    this._addscore = 0;
    this.point = -1;
    this.valueId = 0;

    this.init = function (_userInfo) {
        this.setAccount(_userInfo.account);
        this._socre = 100;
        this.idx = _userInfo.idx;
        this.connect();
        this.time();
    };

    this.setAccount = function (_Account) {
        this._account = _Account;
    };

    this.getAccount = function () {
        return this._account;
    };

    this.EnterLoginServer = function () {
        this._socket.emit('login', {
            userName: this.getAccount(),
            sign: '@@@@youmegame2019`@112233445566@@@@',
            gameId: 1
        })
    };


    this.connect = function () {
        //this._socket = io.connect('http://120.76.200.182:3000');
        ////log.info("连接")
        this._socket = io.connect('http://localhost:13000');
        //this._socket = io.connect('http://192.168.1.170:3000');
        this.initNetWork();
        this.EnterLoginServer();
    };

    //连接游戏服务器
    this.connectGame = function () {
        this._gameSocket = io.connect('http://' + this._ip + ':' + this._port);
        ////log.info(this._ip)
        this.initGameSocket();
        //进入游戏,被服务器分配房间
        this.logingame();
    };

    //进入游戏
    this.logingame = function () {
        if (this._gameSocket && this._userId) {
            this._gameSocket.emit('LoginGame', {userid: this._userId, sign: this._sign})
        }
    };

    //进入房间
    this.loginroom = function () {
        this._gameSocket.emit('LoginRoom', {roomid: 1});
    };

    //接收大厅信息
    this.initNetWork = function () {
        var self = this;
        this._socket.on('connected', function (msg) {
            ////log.info(msg);
        });

        this._socket.on('loginResult', function (result) {
            ////log.info(result)
            if (result.resultid !== 1) {
                self.disconnect();
                return;
            }
            self._sign = result.Obj.sign;
            self._userId = result.Obj.id;
            self._socre = result.Obj.score;
            self._online = true;

        });

        this._socket.on('ServerListResult', function (msg) {

            log.info(self._userId + "进入翻硬币");
            self._ip = "localhost";
            self._port = "16016";

            //进入游戏
            self.connectGame();
        })

    };

    this.down = function () {
        if (this._out) {
            return;
        }
        --this.TotalCount;
    };

    this.time = function () {
        if (!this.timer) {
            var second = 1000;
            this.timer = setInterval(this.down.bind(this), second);
        }
    };

    this.out = function () {
        this.disconnect();
        clearInterval(this.timer);
        this._out = true;
    };

    this.checkPoint = function (cardArray) {
        var point = -1;
        var totol = 0;
        if (cardArray && cardArray.length == 4) {
            for (var i = 0; i < cardArray.length; i++) {
                cardArray[i] = cardArray[i] % 13;
                if (cardArray[i] > 10 && cardArray[i] == 0) {
                    cardArray[i] = 10;
                }
                totol += cardArray[i];
            }
            for (var i1 = 0; i1 < 2 && cardArray.length == 4; ++i1) {
                for (var i2 = i1 + 1; i2 < 3 && cardArray.length == 4; ++i2) {
                    for (var i3 = i2 + 1; i3 < 4 && cardArray.length == 4; ++i3) {
                        if ((cardArray[i1] + cardArray[i2] + cardArray[i3] % 13) % 10 == 0) {
                            //log.info(i1 + " " + i2 + " " + i3);
                            return totol % 10;
                        }
                    }
                }
            }

        }
        return point;

    };


    //接收游戏信息
    this.initGameSocket = function () {
        var self = this;
        this._gameSocket.on('connected', function (msg) {
            ////log.info(msg);
        });

        this._gameSocket.on('loginGameResult', function (msg) {
            ////log.info(msg);
            //结束与登录服务器通信
            // self.loginroom();
        });

        this._gameSocket.on('BetStart', function (msg) {
            //开始下注
            self.sendCardCointimer = setTimeout(self.lottery.bind(self), Math.floor((Math.random() * 8) * 1000) + 1000);
        });

    };

    this.lottery = function () {
        clearInterval(this.sendCardCointimer);
        ////log.info(this._userId);
        try {
            this._gameSocket.emit('lottery', JSON.stringify({
                bet_res: RandomNumBoth(0, 1),
                bet_gold: RandomNumForList(this.chipsConfig)
            }));
        } catch (e) {

        }

    };

    this.disconnect = function () {
        if (this._gameSocket) {
            this._gameSocket.disconnect();
            this._gameSocket = null;
        }
        if (this._socket) {
            this._socket.disconnect();
            this._socket = null;
        }
        this._online = false;
        //log.info(this._userId + "离开游戏")
        clearInterval(this.timer);
    };

    this.isOnline = function () {
        return this.TotalCount > 0 || this.isDown;
    };

    this.init(_userInfo);

};


function RandomNumBoth(Min, Max) {
    //生成指定范围内随机整数
    var Range = Max - Min;
    var Rand = Math.random();
    var num = Min + Math.round(Rand * Range); //四舍五入
    return num;
}


function RandomNumForList(arr) {
    //从指定数组中选取随机值
    return arr[Math.floor((Math.random() * arr.length))]
}

module.exports = fanyingbiUser;